package toman;

import javax.persistence.TemporalType;
import java.sql.SQLException;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * The base class for queries that fetch only a single result TO and do not have parameters.
 * <p>
 * User: zslajchrt
 * Date: 4.5.11
 * Time: 10:15
 */
public abstract class SingleTOQuery<T> implements TOQuery<T> {

    protected final Class<T> toClass;
    protected final TOMan toman;

    protected SingleTOQuery(TOMan toman, Class<T> toClass) {
        this.toman = toman;
        this.toClass = toClass;
    }

    public TOQuery setFirstResult(int index) {
        return this;
    }

    public TOQuery setMaxResults(int max) {
        return this;
    }

    public List<T> getResults() {
        throw new UnsupportedOperationException();
    }

    /**
     * @return the single result raw data object or there is no result for this query
     * @throws NonUniqueResultException if there is more than one result for this query
     * @throws java.sql.SQLException
     */
    protected abstract Object createSingleRawData() throws SQLException;

    public T getSingleResult() {
        try {
            return toman.createTOInstance(toClass, createSingleRawData());
        } catch (SQLException e) {
            throw new IllegalArgumentException(e);
        }
    }

    public Object getInternalQuery() {
        return null;
    }

    public ParameterizedQuery setParameter(String paramName, Object value) {
        throw new UnsupportedOperationException();
    }

    public ParameterizedQuery setParameter(String paramName, Date value, TemporalType type) {
        throw new UnsupportedOperationException();
    }

    public Set<String> getParameterNames() {
        return Collections.emptySet();
    }
}
